<!--
  File Name: LeaveManager.php
  Version 1.0
  O1-31-2011
-->

<?php
require_once('functions.php');

class LeaveManager
{
	public function insertLeaveForm($dateOfFiling, $dateReceived,
									$typeOfLeave, $days, $startDate, $endDate,
									$status, $employeeNumber, $time){
        $link = makeSQLconnection();
      
	if($dateOfFiling == "NULL" && $dateReceived == "NULL") {
			
		$query = sprintf("INSERT INTO `LEAVEFORM` (
				  `Date_of_filing`, `Date_received`, `Type_of_leave`, 
				  `Start_date`, `Days`, `End_date`, `Status`, `Employee_number`, `Hours_minutes`) VALUES (
				  NULL, NULL, '$typeOfLeave', 
				  '$startDate', '$days', '$endDate', '$status', '$employeeNumber', '$time')");

	}
	else if($dateOfFiling == "NULL" && $dateReceived != "NULL") {
		$query = sprintf("INSERT INTO `LEAVEFORM` (
				  `Date_of_filing`, `Date_received`, `Type_of_leave`, 
				  `Start_date`, `Days`, `End_date`, `Status`, `Employee_number`, `Hours_minutes`) VALUES (
				  NULL, '$dateReceived', '$typeOfLeave', 
				  '$startDate', '$days', '$endDate', '$status', '$employeeNumber', '$time')");

	} else if ($dateReceived == "NULL" && $dateOfFiling != "NULL") {
		$query = sprintf("INSERT INTO `LEAVEFORM` (
				  `Date_of_filing`, `Date_received`, `Type_of_leave`, 
				  `Start_date`, `Days`, `End_date`, `Status`, `Employee_number`, `Hours_minutes`) VALUES (
				  '$dateOfFiling', NULL, '$typeOfLeave', 
				  '$startDate', '$days', '$endDate', '$status', '$employeeNumber', '$time')");

	} else {
		$query = sprintf("INSERT INTO `LEAVEFORM` (
				  `Date_of_filing`, `Date_received`, `Type_of_leave`, 
				  `Start_date`, `Days`, `End_date`, `Status`, `Employee_number`, `Hours_minutes`) VALUES (
				  '$dateOfFiling', '$dateReceived', '$typeOfLeave', 
				  '$startDate', '$days', '$endDate', '$status', '$employeeNumber', '$time')");
}
	
        $return = mysql_query($query, $link);
		
        if (!$return) {
			$success = 3;
		} else {
			$success = 1;
		}
        mysql_close($link);
		LeaveFormView::showMessage($success);
		return $success;
		
	}
	
	public function getLeaveDataName($fName, $mName, $lName) {
		$link = makeSQLconnection();
		$q1 = sprintf("SELECT * FROM `EMPLOYEE` WHERE First_name ='$fName' and Middle_name='$mName' and Last_name='$lName'");
		$r1 = mysql_query($q1, $link);
		
		/*if (mysql_num_rows($r1) == 0) {
			echo "Employee Not Found.";
		} else {
			if(mysql_num_rows($r1) == 1) {
				$q1 = sprintf("SELECT * FROM `EMPLOYEE` WHERE Employee_number='".$data['Employee_number']."'");
				$r1 = mysql_query($q1, $link);
			} else {
				echo "More than one employee found.";
			}
		}*/
		
		return $r1;
	}
	
	public function getEmployeeData($employeeNumber) {
		$link = makeSQLconnection();
		
		$q2 = sprintf("SELECT * FROM `EMPLOYEE` WHERE Employee_number='".$employeeNumber."'");		
		$r2 = mysql_query($q2, $link);
		
		return $r2;
	}
	
	public function getLeaveData($employeeNumber, $option) {
		$link = makeSQLconnection();
		
		$q1 = sprintf("SELECT * FROM `LEAVEFORM` WHERE Employee_number='".$employeeNumber."'");
		$r1 = mysql_query($q1, $link);
		
		$q2 = sprintf("SELECT * FROM `EMPLOYEE` WHERE Employee_number='".$employeeNumber."'");		
		$r2 = mysql_query($q2, $link);
		
		if($option=="load") {
			return $r1;
		} else if ($option=="compute") {
			return $r2;
		}
	}
	
	public function getRowData($leaveNumber){
		$link = makeSQLconnection();
		
		$q1 = sprintf("SELECT * FROM `LEAVEFORM` WHERE Leave_number='".$leaveNumber."'");
		$r1 = mysql_query($q1, $link);
		return $r1;
	}

	
	public function updateLeaveForm($leaveNumber, $dateOfFiling, $dateReceived,
									$typeOfLeave, $days, $startDate, $endDate,
									$status, $time, $employeeNumber) {
		$link = makeSQLconnection();
		
		if ($dateOfFiling == "NULL" && $dateReceived != "NULL") {
	
			$query = sprintf("UPDATE `LEAVEFORM` SET `Date_of_filing`=NULL, `Date_received`='$dateReceived', `Type_of_leave`='$typeOfLeave', 
					  `Start_date`='$startDate', `Days`='$days', `End_date`='$endDate', `Status`='$status', `Excess`='0', `Hours_minutes`='$time' WHERE `Leave_number`='$leaveNumber'");					  
		
		} else if ($dateReceived == "NULL" && $dateOfFiling != "NULL") {
		
			$query = sprintf("UPDATE `LEAVEFORM` SET `Date_of_filing`='$dateOfFiling', `Date_received`=NULL, `Type_of_leave`='$typeOfLeave', 
					  `Start_date`='$startDate', `Days`='$days', `End_date`='$endDate', `Status`='$status', `Excess`='0', `Hours_minutes`='$time' WHERE `Leave_number`='$leaveNumber'");					  
		
		} else if ($dateOfFiling == "NULL" && $dateReceived == "NULL") {
				
			$query = sprintf("UPDATE `LEAVEFORM` SET `Date_of_filing`=NULL, `Date_received`=NULL, `Type_of_leave`='$typeOfLeave', 
					  `Start_date`='$startDate', `Days`='$days', `End_date`='$endDate', `Status`='$status', `Excess`='0', `Hours_minutes`='$time' WHERE `Leave_number`='$leaveNumber'");
		
		} else {		
		
			$query = sprintf("UPDATE `LEAVEFORM` SET `Date_of_filing`='$dateOfFiling', `Date_received`='$dateReceived', `Type_of_leave`='$typeOfLeave', 
					  `Start_date`='$startDate', `Days`='$days', `End_date`='$endDate', `Status`='$status', `Excess`='0', `Hours_minutes`='$time' WHERE `Leave_number`='$leaveNumber'");					  
		}
		
		$return = mysql_query($query, $link);
        
		if (!$return) {
			$success = 3;
		} else {
			$success = 2;
		}   
			
			
		
        
		mysql_close($link);
		return $success;
	}
}



?>